package com.cloud.code.内省;

import lombok.SneakyThrows;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.MethodDescriptor;
import java.beans.PropertyDescriptor;

public class Test {

    @SneakyThrows
    public static void main(String[] args) {

        Class<?> aClass = Class.forName("com.cloud.code.内省.User");
        Object instance = aClass.newInstance();
        BeanInfo beanInfo = Introspector.getBeanInfo(aClass);

        MethodDescriptor[] methodDescriptors = beanInfo.getMethodDescriptors();
        for (int i = 0; i < methodDescriptors.length; i++) {
//            System.out.println(methodDescriptors[i].getName());
        }

        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        for (int i = 0; i < propertyDescriptors.length; i++) {
            String name = propertyDescriptors[i].getName();
            if ("userName".equalsIgnoreCase(name))
                propertyDescriptors[i].getWriteMethod().invoke(instance,"luojie");

        }

        System.out.println(((User)instance).getUserName());

    }
}
